﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.Utility;
using AjaxPro;
using System.IO;
using System.Text;
using System.Drawing;

namespace EnvironmentManagerSystem.ExpertManager
{
    public partial class AddNewExperts : System.Web.UI.Page
    {
        public string ImageURL
        {
            get
            {
                if (ViewState["ImageURL"] == null)
                {
                    ViewState["ImageURL"] = string.Empty;
                }
                return ViewState["ImageURL"].ToString();
            }
            set
            {
                ViewState["ImageURL"] = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                txtBind();
                DataBing();
            }
        }

        #region 绑定文本框
        public string id;
        public void txtBind()
        {
            id = Request.QueryString["id"];
            if (id == null) { }
            else
            {
                ExpertData model = Expert.GetModelByID(id)[0];
                txtBirthday.Text = model.Birthday.Trim();
                txtCertNo.Text = model.CardNo.Trim();
                txtCompany.Text = model.Work.Trim();
                txtEMail.Text = model.Email.Trim();
                txtExpertName.Text = model.Name.Trim();
                txtFax.Text = model.Fax.Trim();
                txtMobile.Text = model.Mobile.Trim();
                txtNowCompany.Text = model.NowWork.Trim();
                txtPhone.Text = model.Phone.Trim();
                txtRecommendation.Text = model.Recommendation.Trim();
                if (model.Sex == "男")
                    rbNan.Checked = true;
                else if (model.Sex == "女")
                    rbNv.Checked = true;
                txtTitle.Text = model.Title.Trim();
                TextBox1.Text = model.ExpertMemo.Trim();
                txtBusinessType.Text = model.BusinessType.Trim();
                txtEIAType.Text = model.EIAType.Trim();
                if (model.ImageURL == "")
                    Image1.ImageUrl = "~/NewsImages/images/zhuanjia.png";
                else
                {
                    Image1.ImageUrl = model.ImageURL;
                    ImageURL = model.ImageURL.Replace("\\", "\\\\");
                }
                if (model.ExpertType == "行业专家")
                    cbExpertType1.Checked = true;
                else if (model.ExpertType == "环评专家")
                    cbExpertType2.Checked = true;
                else if (model.ExpertType == "行业专家,环评专家")
                {
                    cbExpertType1.Checked = true;
                    cbExpertType2.Checked = true;
                }
            }
        }
        #endregion

        #region 行业类别，环评类别绑定
        public void DataBing()
        {
            //行业类别
            DataTable ccc = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("14", "%");
            IndustryType1.DataSource = ccc;
            IndustryType1.DataBind();
            IndustryType1.Items.Insert(0, new ListItem("请选择", "0%"));
            //环评类别
            DataTable cccccc = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("7");
            ddlEIAType.DataSource = cccccc;
            ddlEIAType.DataBind();
            ddlEIAType.Items.Insert(0, new ListItem("请选择", "0"));
        }
        protected void tbs001_developprojectbasicinfo_IndustryType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //行业类别2
            IndustryType2.DataSource = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("24", IndustryType1.SelectedValue + "%");
            IndustryType2.DataBind();
            IndustryType2.Items.Insert(0, new ListItem("全部", "0"));
        }
        #endregion

        #region Button_Click
        //行业类别添加按钮
        protected void btnBusinessType_Click(object sender, EventArgs e)
        {
            string BusinessType = "";
            if (IndustryType2.SelectedItem.Text == "全部")
            {
                DataTable dt = EnvironmentManagerSystem.BLL.ConstructionUnit.Gethylb("24", IndustryType1.SelectedValue + "%");
                if (dt.Rows.Count == 0)
                    return;
                foreach (DataRow dr in dt.Rows)
                {
                    BusinessType = dr["Name"].ToString();
                    txtBusinessType.Text = txtBusinessType.Text + BusinessType + ",";
                }
            }
            else
            {
                BusinessType = IndustryType2.SelectedItem.Text;
                txtBusinessType.Text = txtBusinessType.Text + BusinessType + ",";
            }
        }
        //环评类别添加按钮
        protected void btnEIAType_Click(object sender, EventArgs e)
        {
            string EIAType = ddlEIAType.SelectedItem.Text;
            txtEIAType.Text = txtEIAType.Text + EIAType + ",";
        }
        //保存按钮
        public string id1;
        protected void btnOK_Click(object sender, EventArgs e)
        {
            id1 = Request.QueryString["id"];
            ExpertData model = new ExpertData();
            model.Name = txtExpertName.Text.Trim();//专家姓名
            model.Birthday = txtBirthday.Text.Trim();//出生年月
            if (rbNan.Checked == true)//性别
                model.Sex = "男";
            if (rbNv.Checked == true)
                model.Sex = "女";
            model.CardNo = txtCertNo.Text.Trim();//身份证号
            model.Work = txtCompany.Text.Trim();//工作单位
            model.NowWork = txtNowCompany.Text.Trim();//现工作岗位
            model.Title = txtTitle.Text.Trim();//职务/职称
            model.Mobile = txtMobile.Text.Trim();//移动电话
            model.Phone = txtPhone.Text.Trim();//固定电话
            model.Email = txtEMail.Text.Trim();//电子邮箱
            model.Fax = txtFax.Text.Trim();//传真
            model.Recommendation = txtRecommendation.Text.Trim();//推荐单位/人
            model.BusinessType = txtBusinessType.Text.Trim();//行业类别
            model.EIAType = txtEIAType.Text.Trim();//环评类别
            model.ExpertMemo = TextBox1.Text.Trim();//专家简介
            model.ImageURL = ImageURL;
            if (cbExpertType1.Checked == true) //专家类型
                model.ExpertType = "行业专家";
            if (cbExpertType2.Checked == true)
                model.ExpertType = "环评专家";
            if (cbExpertType1.Checked == true && cbExpertType2.Checked == true)
                model.ExpertType = "行业专家,环评专家";
            if (id1 == null)
            {
                if (Expert.Add(model) > 0)
                    Response.Write("<script>alert('添加成功');window.parent.art.dialog.list['新增专家'].close();</script>");
                else
                    Jscript.Alert("添加失败!", this);
            }
            else
            {
                model.ID = Convert.ToInt32(id1);
                if (Expert.Update(model) > 0)
                {
                    //Jscript.Alert("修改成功!", this);
                    //Response.Write("<script>window.parent.location.href = 'ExpertManagerPage.aspx';</script>");
                    Response.Write("<script>alert('修改成功');window.parent.art.dialog.list['修改专家'].close();</script>");
                }
                else
                {
                    Jscript.Alert("修改失败!", this);
                }
            }
        }
        //取消按钮
        protected void btncancel_Click(object sender, EventArgs e)
        {
            Response.Write("<script>window.parent.art.dialog.list['新增专家'].close();</script>");
        }
        #endregion

        #region LinkButton_Click
        protected void lbtnImage_Click(object sender, EventArgs e)
        {
            ImageURL = GetFileUrl(FileUpload1, Image1);
        }
        private string GetFileUrl(FileUpload fuLoad, System.Web.UI.WebControls.Image img)
        {
            string FileUrl = string.Empty;
            if (txtExpertName.Text.Trim() == string.Empty)
                Jscript.Alert("请先填写专家名称", this);
            else
            {
                if (fuLoad.HasFile)
                {
                    bool fileOK = false;
                    string fileExtension = Path.GetExtension(fuLoad.FileName).ToLower();
                    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                        }
                    }
                    if (fileOK)
                    {
                        try
                        {
                            string Suffix = fuLoad.FileName.Split('.')[1];//获取上传文件的后缀
                            string NewName = txtExpertName.Text.Trim() + "." + Suffix;//重命名上传文件
                            if (!Directory.Exists(Server.MapPath(".") + "\\UploadFile\\"))
                            {
                                Directory.CreateDirectory(Server.MapPath(".") + "\\UploadFile\\");
                            }
                            fuLoad.PostedFile.SaveAs(Server.MapPath(".") + "\\UploadFile\\" + NewName);
                            string upLoadPath = "UploadFile\\" + NewName;
                            img.ImageUrl = "UploadFile\\" + NewName;
                            FileUrl = "UploadFile\\\\" + NewName;
                        }
                        catch (Exception)
                        {
                            Jscript.Alert("上传失败！", this);
                        }
                    }
                    else
                    {
                        Jscript.Alert("只能上传图片！", this);
                    }
                }
            }
            return FileUrl;
        }
        #endregion
    }
}